package pl.krakow.ae.knp.wikiboard.gui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import pl.krakow.ae.knp.wikiboard.communication.Connection;
import pl.krakow.ae.knp.wikiboard.gui.board.Board;
import pl.krakow.ae.knp.wikiboard.gui.bundles.Lang;
import pl.krakow.ae.knp.wikiboard.gui.palette.BgColorChanged;
import pl.krakow.ae.knp.wikiboard.gui.palette.ColorChanged;
import pl.krakow.ae.knp.wikiboard.gui.palette.FgColorChanged;
import pl.krakow.ae.knp.wikiboard.gui.palette.OtherPaletteEvent;
import pl.krakow.ae.knp.wikiboard.gui.palette.OtherPaletteEventType;
import pl.krakow.ae.knp.wikiboard.gui.palette.Palette;
import pl.krakow.ae.knp.wikiboard.gui.palette.PaletteEvent;
import pl.krakow.ae.knp.wikiboard.gui.palette.PaletteEventListener;
import pl.krakow.ae.knp.wikiboard.gui.palette.ToolChanged;
import pl.krakow.ae.knp.wikiboard.gui.userslist.UsersList;

/**
 *
 * A class that represents a Graphical User Interface
 * that includes canvas and palette with buttons.
 *
 */
public class GUI extends JPanel implements PaletteEventListener {
    private Board board = null;
    private Palette palette = null;
    private Connection connection = null;
    private UsersList usersList = null;
    private static String userName;
    
    /**
     * Creates a new instance of GUI
     */
    
    public GUI() {
        addComponents();
        layoutComponents();
    }
    
    private void addComponents() {
        usersList = new UsersList();
        board = new Board();
        palette = new Palette(this);
        connection = new Connection(this);
    }
    
    private void layoutComponents() {
        add(usersList);
        add(board.getBoardView());
        add(palette);
    }
    
    /** If palette sends some events...
     * @param event event sent by Palette
     */
    public void paletteModified(PaletteEvent event) {
        
        if (event instanceof OtherPaletteEvent) {
            try {
                OtherPaletteEvent e = (OtherPaletteEvent)event;
                if (e.getPaletteEventType() == OtherPaletteEventType.UNDO)
                    board.undo();
                else if (e.getPaletteEventType() == OtherPaletteEventType.REDO)
                    board.redo();
                else if (e.getPaletteEventType() == OtherPaletteEventType.CLEAR)
                    board.clearTable();
                else if (e.getPaletteEventType() == OtherPaletteEventType.CONNECT) {
                    
                    userName = (String)JOptionPane.showInputDialog(
                            null,Lang.get("enter_user_name"), Lang.get("user_name"),
                            JOptionPane.INFORMATION_MESSAGE,null,null,"user");
                    connection.connect( palette.getTxAddress(), getUserName());
                    if( !connection.isConnected() )
                        return;
                    connection.addListener(usersList);
                } else if (e.getPaletteEventType() == OtherPaletteEventType.DISCONNECT){
                    connection.disconnect();
                } else if (e.getPaletteEventType() == OtherPaletteEventType.USERNAME_EXISTS) {
                    
                    userName = (String)JOptionPane.showInputDialog(
                            null, Lang.get("user_name_already_exist"), Lang.get("name_busy"),
                            JOptionPane.WARNING_MESSAGE,null,null,"user");
                    connection.connect( palette.getTxAddress(), getUserName());
                    if( !connection.isConnected() )
                        return;
                    connection.addListener(usersList);
                }
            }  catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            
            
        }
        
        else if (event instanceof ColorChanged) {
            ColorChanged e = (ColorChanged)event;
            if (e instanceof FgColorChanged)
                board.setFgColor(e.getColor());
            else if (e instanceof BgColorChanged)
                board.setBgColor(e.getColor());
        } else if (event instanceof ToolChanged) {
            ToolChanged e = (ToolChanged)event;
            board.setTool(e.getToolType());
            //board.getBoardView().setCursor(e.getToolType().getCursor());
        }
        board.repaint();
    }
    
    public boolean isConnected() {
        return connection.isConnected();
    }
    
    public static String getUserName() {
        return userName;
    }
}
